﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace Rally_like_a_madman.Data
{
    [Table]
    public class RouteWaypointLink : INotifyPropertyChanged, INotifyPropertyChanging
    {
        public event PropertyChangingEventHandler PropertyChanging;
        public event PropertyChangedEventHandler PropertyChanged;

        private int idRouteWaypointLinkValue;
        private int waypointIndexValue;
        private Nullable<int> idRoute;
        private Nullable<int> idWaypoint;
        private EntityRef<Route> routeValue;
        private EntityRef<Waypoint> waypointValue;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int IDRouteWaypointLink
        {
            get
            {
                return idRouteWaypointLinkValue;
            }
            private set
            {
                NotifyPropertyChanging("IDRouteWaypointLink");
                idRouteWaypointLinkValue = value;
                NotifyPropertyChanged("IDRouteWaypointLink");
            }
        }

        [Column(Storage = "idRoute", DbType = "Int")]
        public int? IDRoute
        {
            get
            {
                return this.idRoute;
            }
            set
            {
                NotifyPropertyChanging("IDRoute");
                this.idRoute = value;
                NotifyPropertyChanged("IDRoute");
            }
        }

        [Column(Storage = "idWaypoint", DbType = "Int")]
        public int? IDWaypoint
        {
            get
            {
                return this.idWaypoint;
            }
            set
            {
                NotifyPropertyChanging("IDWaypoint");
                this.idWaypoint = value;
                NotifyPropertyChanged("IDWaypoint");
            }
        }

        [Column]
        public int WaypointIndex
        {
            get
            {
                return waypointIndexValue;
            }
            set
            {
                NotifyPropertyChanging("WaypointIndex");
                waypointIndexValue = value;
                NotifyPropertyChanged("WaypointIndex");
            }
        }

        [Association(IsForeignKey = true, Storage = "routeValue", ThisKey = "IDRoute")]
        public Route Route
        {
            get
            {
                return routeValue.Entity;
            }
            set
            {
                NotifyPropertyChanging("Route");
                routeValue.Entity = value;
                NotifyPropertyChanged("Route");
            }
        }

        [Association(IsForeignKey = true, Storage = "waypointValue", ThisKey = "IDWaypoint")]
        public Waypoint Waypoint
        {
            get
            {
                return waypointValue.Entity;
            }
            set
            {
                NotifyPropertyChanging("Waypoint");
                waypointValue.Entity = value;
                NotifyPropertyChanged("Waypoint");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return waypointValue.Entity.ToString();
        }
    }
}
